package showtime.framework.algorithm.collections
{
	
	/** 
	 * @project DelphiFrmework
	 * @author Delphi
	 * @date 2011-6-2
	 *
	 */
	
	public class LRUMap
	{
		protected var cache:HashMap = null;
		
		protected var cacheList:Array = null;
		
		private var _capacity:int = 3;
		
		public function LRUMap()
		{
			cache = new HashMap();
			
			cacheList = [];
		}
		
		public function get capacity():int
		{
			rturn _capacity;
		}
		
		public function getSize():int
		{
			return cache.getValues().length;
		}
		
		private function moveToTop(id:*):void
		{
			cacheList.unshift(id, 0);
			
			if (getSize() >= capacity)
			{
				cache.remove(cacheList.pop());
				
			}
		}
		
		public function get(id:*):*
		{
			if (cache.containsKey(id))
			{
				moveToTop(id);
				
				return cache.getValue(id);
			}
			else
			{
				return;
			}
		}
		
		public function put(id:*, value:*):void
		{
			if (getSize() < capacity)
			{
				if (!cache.containsKey(id))
				{
					cacheList.unshift(value);
				}
				
				cache.put(id, value);
			}
			else
			{
				cache.put(id, value);
				
				moveToTop(id);
			}
		}
	}
}